package com.jiepos.mpos.biz.review.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCustomBankcardDao;
import com.jiepos.mpos.common.dao.SettleAccountDao;
import com.jiepos.mpos.common.entity.App_Custom_BankcardEntity;
import com.jiepos.mpos.common.entity.MemReviewEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.StringUtils;
import com.jiepos.mpos.inter.intopieces.service.IntoPieces;

/**
 * 
 * 结算信息变更待办事项管理
 * 
 * @ProjectName: [mpos]
 * @Package com.jiepos.mpos.biz.review.service
 * @author 张磊
 * @version 1.0
 * @since 2017年9月26日 下午5:04:24
 * @history（历次修订内容、修订人、修订时间等） @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Service
public class SettleAccountService extends BaseService {

	@Autowired
	private SettleAccountDao settleAccountDao;
	@Autowired
	private AppCustomBankcardDao appCustomBankcardDao;
	@Autowired
	private IntoPieces intoPieces;
	
	/**
	 * 获取待办结算事项Json
	 * @param pagination
	 * @param request
	 * @param memReviewEntity
	 * @return
	 */
	public Map getAccountReviewList(Pagination pagination, HttpServletRequest request,
			MemReviewEntity memReviewEntity) {

		StringBuilder sb = new StringBuilder();
		List list = new ArrayList<>();
		sb.append("select t1.audit_st,t1.bankcard_uuid AS uuid,t1.custom_uuid,TO_CHAR (t1.rec_crt_ts,'yyyy-mm-dd hh24:mi:ss') AS rec_crt_ts,")
			.append(" TO_CHAR (t1.rec_upd_ts,'yyyy-mm-dd hh24:mi:ss') AS rec_upd_ts,t1.rec_upd_usr_id,")
			.append("aa.custom_no,aa.phone,aa.name,aa.realname_auth_st,aa.rec_st,aa.certificate_no,aa.supcustom,aa.ins_uuid,aa.ins_cd,aa.ins_cn_nm,aa.insname,aa.areaname FROM ")
			.append(Table.T_APP_CUSTOM_BANKCARD)
			.append(" t1 LEFT JOIN (SELECT t2.custom_uuid,t2.custom_no,CONCAT(RPAD (SUBSTR (t2.phone, 1, 3),LENGTH (t2.phone) - 4,'*'),SUBSTR (t2.phone,LENGTH (t2.phone) - 3,4)) phone,t2.name,t2.realname_auth_st,t2.rec_st,")
			.append(" CONCAT (RPAD (SUBSTR (t2.certificate_no, 1, 6),LENGTH (t2.certificate_no) - 4,'*'), ")
			.append(" SUBSTR (t2.CERTIFICATE_NO,LENGTH (t2.CERTIFICATE_NO) - 3,4)) AS \"CERTIFICATE_NO\",")
			.append(" NVL2(t3.login_id,CONCAT(RPAD (SUBSTR (t3.login_id, 1, 3),LENGTH (t3.login_id) - 4,'*'),SUBSTR (t3.login_id,LENGTH (t3.login_id) - 3,4))||'-'||t3.NAME,'') supcustom,t4.ins_uuid,t4.ins_cd,t4.ins_cn_nm,NVL2(t4.ins_cd,t4.ins_cd||'-'||t4.ins_cn_nm,'') insname,")
			.append(" NVL2(t5.area_cd,t5.area_cd||'-'|| t5.area_cn_nm, '') areaname FROM ").append(Table.T_APP_CUSTOM_INF).append(" t2 LEFT JOIN ")
			.append(Table.T_APP_CUSTOM_INF).append(" t3 ON t2.sup_custom_uuid = t3.custom_uuid ")
			.append(" LEFT JOIN ").append(Table.T_APP_INS_INF).append("  t4 ON t2.ins_uuid = t4.ins_uuid ")
			.append(" LEFT JOIN ").append(Table.T_APP_AREA_CD).append("  t5 ON t2.area_uuid = t5.area_uuid ")
			.append(" ) aa ON t1.custom_uuid = aa.custom_uuid ")
			.append(" WHERE t1.audit_st = ? AND t1.acct_attr = ? AND t1.rec_st != ? and t1.custom_uuid in ( ")
			.append(" select custom_uuid from "+Table.T_APP_CUSTOM_INF +" where REALNAME_AUDIT_ST = ? and rec_st != ? ) ");
		list.add(SystemConstant.REVIEW_ST_0);
		list.add(SystemConstant.ACCT_ATTR_0);
		list.add(SystemConstant.REC_ST_0);
		list.add(SystemConstant.REVIEW_ST_1);
		list.add(SystemConstant.REC_ST_0);
		if (!StringUtils.isBlank(memReviewEntity.getCustom_no())) {
			sb.append(" and aa.custom_no like ? ");
			list.add("%" + memReviewEntity.getCustom_no() + "%");
		}
		if (!StringUtils.isBlank(memReviewEntity.getPhone())) {
			sb.append(" and aa.phone like ? ");
			list.add("%" + memReviewEntity.getPhone() + "%");
		}
		if (!StringUtils.isBlank(memReviewEntity.getName())) {
			sb.append(" and aa.name like ? ");
			list.add("%" + memReviewEntity.getName() + "%");
		}
		if (!StringUtils.isBlank(memReviewEntity.getIns_cd())) {
			sb.append(" and aa.ins_cd like ? ");
			list.add("%" + memReviewEntity.getIns_cd() + "%");
		}
		if (!StringUtils.isBlank(memReviewEntity.getIns_cn_nm())) {
			sb.append(" and aa.ins_cn_nm like ? ");
			list.add("%" + memReviewEntity.getIns_cn_nm() + "%");
		}
		if (!StringUtils.isBlank(memReviewEntity.getCertificate_no())) {
			sb.append(" and aa.certificate_no like ? ");
			list.add("%" + memReviewEntity.getCertificate_no() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = super.getCustomAuthSql(request, pagination, "aa", null);
		pagination.queryString = pagination.queryString+" order by t1.rec_upd_ts desc ";
		return settleAccountDao.getPaginationMap(pagination, "t1", list);
	}

	/**
	 * 获取待办事项数
	 * @param request
	 * @return
	 */
	public Integer getAccountReviewNum(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		List list = new ArrayList<>();
		sb.append(" select bankcard_uuid from ").append(Table.T_APP_CUSTOM_BANKCARD);
		sb.append(" where acct_attr = ? and rec_st != ? and audit_st = ? and CUSTOM_UUID in (");
		sb.append(" select custom_uuid from ").append(Table.T_APP_CUSTOM_INF);
		sb.append(" where realname_audit_st = ? and rec_st != ? and ins_uuid IN ( ");
		sb.append(" select tt.ins_uuid from ").append(Table.T_APP_INS_INF);
		sb.append("  tt START WITH tt.ins_uuid = ? CONNECT BY PRIOR tt.ins_uuid = tt.sup_ins_uuid ))");
		
		list.add(SystemConstant.ACCT_ATTR_0);
		list.add(SystemConstant.REC_ST_0);
		list.add(SystemConstant.REVIEW_ST_0);
		list.add(SystemConstant.REVIEW_ST_1);
		list.add(SystemConstant.REC_ST_0);
		list.add(String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID)));
		
		List<String> result = appCustomBankcardDao.queryForList(sb.toString(), list);
		if (CollectionUtil.isEmpty(result)) {
	         return 0;
	    } else {
	         return result.size();
	    }
	}

	/**
	 * 审核通过操作
	 * 
	 * @param audit_comments
	 * @param bankcard_uuid
	 * @param map
	 */
	public void accountReviewPass(String audit_comments, String bankcard_uuid, Map map) {
		if(StringUtils.isBlank(audit_comments)){
			audit_comments = "审核通过";
		}
		String customerUuid = settleAccountDao.reviewAccount(audit_comments, bankcard_uuid, map, true);
		intoPieces.modifyAccountByUuid(customerUuid, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
	}

	/**
	 * 退回修改操作
	 * 
	 * @param audit_comments
	 * @param bankcard_uuid
	 * @param map
	 */
	public void accountReviewBack(String audit_comments, String bankcard_uuid, Map map) {
		settleAccountDao.reviewAccount(audit_comments, bankcard_uuid, map, false);
	}
	
	/**
	 * 判断该卡号是否已被审核
	 * @param bankcard_uuid
	 * @return
	 */
	public String checkBankcardInfo(String bankcard_uuid){
		//判断卡号是否已被审核
		String param1[] = {"bankcard_uuid","audit_st","rec_st"};
		String param2[] = {bankcard_uuid,SystemConstant.REVIEW_ST_0,SystemConstant.REC_ST_1};
		App_Custom_BankcardEntity app_Custom_BankcardEntity = appCustomBankcardDao.findByParamses(param1,param2);
		if(app_Custom_BankcardEntity == null){
			return SystemConstant.ERROR;
		}
		return SystemConstant.SUCCESS;
	}
}
